grammar Expr;

@header {
package antlr;

import testaux.*;
import java.util.Arrays;
}

expr returns [AST value]
    :   e=term {$value = $e.value;}
        (   '+' e=term {$value = new AST(AST.ADD, null, Arrays.asList($value, $e.value));}
        |   '-' e=term {$value = new AST(AST.MIN, null, Arrays.asList($value, $e.value));}
        )*
    ;

term returns [AST value]
    :   e=factor {$value = $e.value;}
        ('*' e=factor {$value = new AST(AST.MUL, null, Arrays.asList($value, $e.value));})*
    ; 

factor returns [AST value]
    :   INT {$value = new AST(AST.INTCONST, new Integer($INT.text));}
    |   IDENT {$value = new AST(AST.IDENT, new String($IDENT.text));}
    |   '(' expr ')' {$value = $expr.value;}
    ;


IDENT
    : ('a'..'z' | 'A'..'Z') ('a'..'z' | '0'..'9')* ;

INT : '0'..'9'+ ;

WS  :   (' '|'\t')+ {skip();} ;